package simplenotessyncsrv;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class SyncSrv extends Thread {

    BufferedReader br = null;
    PrintWriter pw = null;
    Boolean Running = true;

    @Override
    public void run() {
        ServerSocket srvsocket = null;

        try {
            srvsocket = new ServerSocket(32115);
            while (Running) {
                System.out.println("Accepting connections. Waiting command.");
                Socket socket = srvsocket.accept();
                ParseCommand(socket);
            }
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }

        try {
            srvsocket.close();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }

    public void Stop() {
        if (Running) {
            try {
                Socket fakesocket = new Socket("localhost", 32115);
                PrintWriter fpw = new PrintWriter(fakesocket.getOutputStream(), true);
                fpw.println("Stop");
                fpw.close();
                fakesocket.close();
            } catch (UnknownHostException ex) {
                System.out.println(ex.toString());
            } catch (IOException ex) {
                System.out.println(ex.toString());
            }
        }
    }

    private void ParseCommand(Socket socket) throws IOException {
        br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        pw = new PrintWriter(socket.getOutputStream(), true);

        String command = br.readLine();
        System.out.println("Command: " + command);

        if (command.equals("Stop")) {
            Running = false;
        }

        if (command.equals("SyncOne")) {
            TaskObjs.LoadTaskList();
            SyncOne();
            TaskObjs.tasklist.clear();
        }

        br.close();
        pw.close();
        socket.close();
    }

    private void SyncOne() throws IOException {
        String id = br.readLine();
        String ct = br.readLine();
        String ft = br.readLine();
        String txt = br.readLine();
        String dn = br.readLine();
        String tek = br.readLine();
        String cht = br.readLine();

        TaskObj gta = new TaskObj(id, ct, ft, txt, dn, tek, cht);

        for (TaskObj ta : TaskObjs.tasklist) {
            if (ta.getTaskID().equals(gta.getTaskID())) {
                int cocht = ta.getChangedTime().compareTo(gta.getChangedTime());
                if (cocht == 0) {
                    System.out.println("=");
                    pw.println("Ok");
                    return;
                }
                if (cocht < 0) {
                    System.out.println("<");
                    TaskObjs.tasklist.set(TaskObjs.tasklist.indexOf(ta), gta);
                    TaskObjs.SaveTaskList();
                    pw.println("Ok");
                    return;
                }
                if (cocht > 0) {
                    System.out.println(">");
                    pw.println("Update");
                    Long l;
                    l = ta.getTaskID();
                    pw.println(l.toString());
                    l = ta.getCreationTime().getTimeInMillis();
                    pw.println(l.toString());
                    l = ta.getFinishTime().getTimeInMillis();
                    pw.println(l.toString());
                    pw.println(getHexString(ta.getTaskText()));
                    pw.println(ta.getDone().toString());
                    pw.println(ta.getTekuchka().toString());
                    l = ta.getChangedTime().getTimeInMillis();
                    pw.println(l.toString());
                    return;
                }
            }
        }

        System.out.println("Adding new object...");
        TaskObjs.tasklist.add(gta);
        TaskObjs.SaveTaskList();
        pw.println("Ok");
    }

    private String getHexString(String str) {
        String hexstr = "";
        int i = 0;
        while (i < str.length()) {
            Integer ic = str.codePointAt(i);
            hexstr = hexstr + Integer.toHexString(ic);
            hexstr = hexstr + "|";
            i++;
        }
        return hexstr;
    }
}
